package tests;

import static org.junit.Assert.*;

import java.util.LinkedList;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import recipie.core.Ingredient;
import recipie.core.IngredientCollection;
import recipie.core.MesureUnit;

import category.Category;
import database.Repository;

public class DatabaseIngredientTest 
{
	private static Ingredient ingredient_1 = null;
	private static Ingredient ingredient_2 = null;
	private static Ingredient ingredientWithCategories = null;
	private static Category category_1 = null;
	private static Category category_2 = null;
	
	@BeforeClass
	public static void setUp() 
	{
	  ingredient_1 = new Ingredient("ing_1", MesureUnit.g);
	  ingredient_2 = new Ingredient("ing_2", MesureUnit.ml);
	  ingredientWithCategories = new Ingredient("ing_3", MesureUnit.kg);
	  category_1 = new Category("category_1");
	  category_2 = new Category("category_2");
	}
	
	@Test
	public void insertIngredient1() 
	{
	  assertTrue(Repository.insertIngredient(ingredient_1));
      assertTrue(Repository.existsIngredient(ingredient_1.getName()));
	}
	
	@Test
	public void insertIngredient2() 
	{
	  assertTrue(Repository.insertIngredient(ingredient_2));
      assertTrue(Repository.existsIngredient(ingredient_2.getName()));
	}
	
	@Test
	public void insertIngredientWithCategories() 
	{
	  Repository.insertCategory(category_1);
	  Repository.insertCategory(category_2);
	  LinkedList<Category> categories = new LinkedList<Category>();
	  categories.add(category_1);
	  categories.add(category_2);
	  
	  ingredientWithCategories.setCategories(categories);
	  
	  assertTrue(Repository.insertIngredient(ingredientWithCategories));
      assertTrue(Repository.existsIngredient(ingredientWithCategories.getName()));
      
      IngredientCollection ingredientCollection = Repository.loadIngredients();
      Ingredient ingredient = ingredientCollection.getIngredients().get("ing_3");
	  assertTrue(ingredient.getCategories().get(0).getName().equals("category_1"));
	  assertTrue(ingredient.getCategories().get(1).getName().equals("category_2"));
	}
	
	@Test
	public void loadIngredients() 
	{
	  IngredientCollection ingredientCollection = Repository.loadIngredients();
	  Ingredient ingredient_11 = ingredientCollection.getIngredients().get("ing_1");
	  Ingredient ingredient_22 = ingredientCollection.getIngredients().get("ing_2");
	  Ingredient ingredient_33 = ingredientCollection.getIngredients().get("ing_3");
	  assertTrue(ingredient_11 != null);
	  assertTrue(ingredient_22 != null);
	  assertTrue(ingredient_33 != null);
	  assertTrue(ingredient_11.getUnit().equals(MesureUnit.g));
	  assertTrue(ingredient_22.getUnit().equals(MesureUnit.ml));
	  assertTrue(ingredient_33.getUnit().equals(MesureUnit.kg));
	}
    
    @AfterClass
   	public static void cleanUp() 
   	{
      Repository.deleteIngredient(ingredient_1);
      Repository.deleteIngredient(ingredient_2);
      Repository.deleteIngredient(ingredientWithCategories);
      Repository.deleteCategory(category_1);
      Repository.deleteCategory(category_2);
   	}

}
